package com.exploadingelephants.views;

import android.graphics.Canvas;
import android.graphics.Color;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.SurfaceView;

import com.exploadingelephants.engine.PlayLoopThread;
import com.exploadingelephants.engine.PlayViewCallback;
import com.exploadingelephants.engine.PlaygroundActivity;
import com.exploadingelephants.engine.PlaygroundGestures;

/**
 * This is the playground
 */
public class PlaygroundView extends SurfaceView {
    /** The main game loop */
    private final PlayLoopThread playLoopThread;

    /** We will forward all our touch moves to the gesture detector */
    private final GestureDetector gestureDetector;

    /**
     * @param playgroundActivity
     */
    public PlaygroundView(PlaygroundActivity playgroundActivity) {
	super(playgroundActivity);
	this.playLoopThread = new PlayLoopThread(playgroundActivity, this);
	this.gestureDetector = new GestureDetector(playgroundActivity,
		new PlaygroundGestures(playgroundActivity));
	this.gestureDetector.setIsLongpressEnabled(false);

	// PlayViewCallback will be called, once the playground is ready
	getHolder().addCallback(
		new PlayViewCallback(playgroundActivity, playLoopThread));
    }

    /**
     * Repaint background in every frame
     * 
     * @param canvas
     */
    public void updateBackground(Canvas canvas) {
	// default background
	canvas.drawColor(Color.BLACK);
    }

    /**
     * Our gestureDetector will do all our touch events
     */
    @Override
    public boolean onTouchEvent(MotionEvent event) {
	return gestureDetector.onTouchEvent(event);
    }
}
